//
//  SLCertificationListComponent.m
//  SulitLoanDemo
//
//  Created by 虞嘉伟 on 2025/2/20.
//  
  

#import "SLCertificationListComponent.h"
#import <YYKit.h>
#import <Masonry.h>
#import "SLCertificationItemComponent.h"


@interface SLCertificationListComponent ()
@property (nonatomic, strong) UIStackView *stackView;
@end
@implementation SLCertificationListComponent

- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        
        [self addSubview:self.bgView];
        [self addSubview:self.progressView];
        [self addSubview:self.stackView];
        
        [self.bgView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.edges.equalTo(self);
        }];
        [self.progressView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.equalTo(self.bgView).inset(20);
            make.top.equalTo(self.bgView).inset(20);
            make.height.mas_equalTo(60);
        }];
        
        [self.stackView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.equalTo(self.bgView).inset(20);
            make.top.equalTo(self.progressView.mas_bottom).offset(20);
            make.bottom.equalTo(self);
        }];
    }
    return self;
}

- (void)layoutSubviews {
    [super layoutSubviews];
    [self.bgView roundTopCornerRadius:20];
}

- (void)setItems:(NSArray *)items {
    _items = items;
    [self.stackView removeAllSubviews];
    for (NSDictionary *dict in items) {
        SLCertificationItemComponent *item = [SLCertificationItemComponent new];
        
        item.isComplete = [dict[@"contained"] sl_intValue] == 1;
        item.indexLabel.text = @(1+[items indexOfObject:dict]).stringValue;
        item.titleLabel.text = dict[@"conception"];
        [item.icon sl_setImageWithURL:dict[@"vacant"]];
        @weakify(self);
        [item jk_addTapActionWithBlock:^(UIGestureRecognizer *gestureRecoginzer) {
            @strongify(self);
            !self.itemClickCallback ?: self.itemClickCallback([items indexOfObject:dict]);
        }];
        
        [self.stackView addArrangedSubview:item];
        NSUInteger i = [items indexOfObject:dict];
        [item mas_makeConstraints:^(MASConstraintMaker *make) {
            make.height.mas_equalTo(54);
            make.right.equalTo(self.stackView).inset(20*i);
        }];
    }
}



- (UIView *)bgView {
    if (!_bgView) {
        _bgView = [UIView new];
        _bgView.backgroundColor = UIColor.whiteColor;
    }
    return _bgView;
}

- (UIStackView *)stackView {
    if (!_stackView) {
        _stackView = [UIStackView new];
        _stackView.axis = UILayoutConstraintAxisVertical;
        _stackView.spacing = 20;
    }
    return _stackView;
}

- (SLCertificationProcessItemComponent *)progressView {
    if (!_progressView) {
        _progressView = [SLCertificationProcessItemComponent new];
    }
    return _progressView;
}

@end
